package com.jt.service;

import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;


import javax.xml.transform.Result;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Author: Li Yu
 * @Date: 2020/11/18 11:10
 * 注释部分：实现修改邮箱功能
 */
@Service
public class UserServiceImpl implements UserService{
    @Override
    public List<User> testHttpClient() {
        //List<LinkedHashMap<String,Object>> userList=new ArrayList<>();
        List<User> userList=new ArrayList<>();
        String url="http://sso.jt.com/user/testHttpClient";
        HttpClient httpClient= HttpClients.createDefault();
        HttpGet httpGet=new HttpGet(url);
        try {
            HttpResponse httpResponse=httpClient.execute(httpGet);
            HttpEntity entity=httpResponse.getEntity();
            String result= EntityUtils.toString(entity,"UTF-8");
            userList= ObjectMapperUtil.toObject(result, userList.getClass());
            /*List<User> userList2=new ArrayList<>();
             for (LinkedHashMap<String,Object> map : userList){
                    User userTemp = new User();
                    userTemp.setId( Long.parseLong(map.get("id")+""));
                    userTemp.setUsername((String)map.get("username"));
                    userTemp.setPassword((String)map.get("password"));
                    userTemp.setPhone((String)map.get("phone"));
                    userTemp.setEmail((String)map.get("phone"));
                    userList2.add(userTemp);
                }*/
            //return userList2;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return userList;
    }
}
